package com.iris.live.services.report.sources;

import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.InsuranceCheckRepositorySql;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import com.iris.live.services.services.DataCheckService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据核检-保险专员导出数据源
 * @author wangzhenyan
 *         2017/5/25 13:59
 */
@Component("exportInsuranceRankingSource")
public class ExportInsuranceRankingSource implements IExcelReportSource{

    @Resource
    private InsuranceCheckRepositorySql insuranceCheckRepositorySql;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商_数据核验排名_保险专员E2","经销商_数据核验排名_保险专员E2",true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> rows = new ArrayList<>();
        //标题行前的空行以及标题行
        for (int index = 0 ; index < 6 ; index++ ) {
            rows.add(new Row());
        }
        //数据行
        int index = 1;
        PageData pageData = insuranceCheckRepositorySql.getRankingOfInsurance((DataCheckService.RankingRequest) context.get("req"));
        if(pageData != null) {
            List datas = pageData.getData();
            if (datas != null) {
                for (Object o : datas) {
                    rows.add(new Row(getDataCells(index++,o)));
                }
            }
        }

        return rows;
    }

    /**
     * 获取数据行
     * @param rowIndex 数据行号
     * @param o 数据
     * @return 返回
     */
    private Cell[] getDataCells(int rowIndex, Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[3];
        cells[0] = new Cell(rowIndex + "", 0);   //排名
        cells[1] = new Cell(StringUtils.isNullOrBlank(map.get("distributor")) ? "" : map.get("distributor").toString(), 1); //所属经销商
        cells[2] = new Cell(StringUtils.isNullOrBlank(map.get("score")) ? "" : map.get("score").toString(), 2); //分数
        return cells;
    }
}
